<?php

/**
 * Html Helper class file.
 *
 * Simplifies the construction of HTML elements.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake.libs.view.helpers
 * @since         CakePHP(tm) v 0.9.1
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::import('Helper', 'Form');
define('CATEGORY_MAX_LEVEL', 5);
define('PARENT_ID', 0);

class CategoryHelper extends FormHelper {

    public $Model = null;
    public $helpers = array('Html');
    public $Option = array();
    public $pathWay = array();
    public function  __construct(View $View, $settings = array()) {
        App::import('Model', 'Catproduct');
        $this->model = new Catproduct();
        parent::__construct($View, $settings);
    }
    function build_list($list_name, $select_id = 0, $disable_id = array(), $multiple = false, $parent_id = PARENT_ID, $attr = 'onchange="document.adminForm.submit();"') {
        $this->Option['0'] = '-- Chọn 1 Category --';
        $this->get($parent_id, $select_id, $disable_id);
        return $this->input($list_name, array('options' => $this->Option, 'label' => false));
    }

    function build_pathway($parent_id = PARENT_ID, $showRoot = true, $symbol = '—>', $nolink = false) {
        $this->pathWay  =   array();
        $this->_build_pathway($parent_id, $nolink);
        if ($showRoot) {
            if (!$nolink) {
                $this->pathWay = array_merge($this->pathWay, array($this->Html->link('Root', array('action'=>'index', 'parent_id'=>PARENT_ID))));
            }
            else
                $this->pathWay = array_merge($this->pathWay, array('Root'));
        }
        $this->pathWay = array_reverse($this->pathWay);
        if(sizeof($this->pathWay) > 1){
            return @implode($symbol, $this->pathWay);
        }else{
            return;
        }
        
    }

    function _build_pathway($parent_id, $nolink = false) {
        $dbrow = $this->model->getParent($parent_id);
        if (!is_array($dbrow) || sizeof($dbrow) == 0) {
            return;
        }
        if (!$nolink) {
            $this->pathWay[] = $this->Html->link($dbrow[$this->model->alias]['title'], array('controller'=>'product_categories', 'action'=>'index', 'parent_id' =>  $dbrow[$this->model->alias]['id']));
        }
        else
            $this->pathWay[] = $dbrow[$this->model->alias]['title'];
        $this->_build_pathway($dbrow[$this->model->alias]['parent_id'], $nolink);
    }

    /**
     * get data from category table
     *
     * @param int $parent_id
     * @param int $select_id
     * @param int $disable_id
     * @param int $level
     * @param string $callback_method method name will process this data return
     */
    function get($parent_id, $select_id, $disable_array , $level = 0, $only_parent = false, $callback_method = '_option') {

        //get childs
        $dbrows = $this->model->getChild($parent_id);
        //create items array which will be disabled

        if ($only_parent) {
            return $dbrows;
        }
        foreach ($dbrows as $obj) {
            $disable_id = 0;
            //disable alll it's child
            //if($obj->parent_id == $disable_id && $disable_id != 0 || $obj->level == CATEGORY_MAX_LEVEL)
            if (in_array($obj[$this->model->alias]['id'], $disable_array) || $obj[$this->model->alias]['level'] == CATEGORY_MAX_LEVEL) {
                $disable_id = $obj[$this->model->alias]['id'];
            }
            $this->$callback_method($obj, $level, $select_id, $disable_id);

            $this->get($obj[$this->model->alias]['id'], $select_id, $disable_array, ++$level, $only_parent);
            $level--;
        }
    }

    public function _option() {
        $args = func_get_args();
        $disable = (bool) ($args[0][$this->model->alias]['id'] == $args[3]);
        $selected = (bool) ($args[0][$this->model->alias]['id'] == $args[2]);
        $space = '';
        for ($i = 0; $i < $args[1]; $i++) {
            $space .= '------';
        }
        $this->Option[$args[0][$this->model->alias]['id']] = array(
            'name' => $space . $args[0][$this->model->alias]['title'],
            'value' => $args[0][$this->model->alias]['id'],
            'disabled' => $disable,
            'selected' => $selected,
        );
    }

    public function setModel($model) {
        if (is_object($model)) {
            $this->model = $model;
        }
    }

}
